package com.xb.loan.util.binaryConvert;


import com.xb.loan.util.binaryConvert.parse.ParsePointer;

import java.util.List;

public class ConvertUtil {
	
	// 读取长度字段，如果小于255，则为第一个字节的内容，如果大于255，则读取后面4个字节的内容
	public static int readSizes(byte[] src,ParsePointer pointer){
		
		int sizes = readUnsignByte(src[pointer.getPointer()]);
		// 指针向后移动一个字节
		pointer.move(1);
		
		if(sizes == 255){
			// 读取后面4个字节的内容
			sizes = readInt(src, pointer.getPointer());
			// 指针向后移动4个字节
			pointer.move(4);
		}
		
		return sizes;
		
	}
	
	// 格式化长度
	public static byte[] convertSizes(int size){
		
		byte[] sizes = null;
		
		if(size >= 255){
			
			sizes = new byte[5];
			
			sizes[0] = (byte) 255;
			
			putInt(sizes, 1, size);
		} else {
			
			sizes = new byte[]{(byte) size};
		}
		
		return sizes;
	}
	
	// 读取无符号字节长度
	public static int readUnsignByte(byte b){
		
		return b & 0xff;
	}
	
	// 读取int
	public static int readInt(byte[] src, int off) {

		int num = (src[off] << 24 & 0xFF000000)
				| (src[off + 1] << 16 & 0xFF0000)
				| (src[off + 2] << 8 & 0xFF00) | (src[off + 3] & 0xFF);

		return num;
	}
	
	// 读取short
	public static short readShort(byte[] src, int off) {

		short num =  (short) ((src[off + 0] << 8 & 0xFF00) | (src[off + 1] & 0xFF));

		return num;
	}
	
	public static int putShort(byte[] src, int index, short b){
		
		for (int i = 0; i < 2; i++) {

			src[index + i] = (byte) (b >> 8 * (1 - i) & 0xFF);

		}

		return index + 2;
	}
	
	// 读取long
	public static long readLong(byte[] bb, int index) {
		
		return ((((long) bb[index + 0] & 0xff) << 56)  
                | (((long) bb[index + 1] & 0xff) << 48)  
                | (((long) bb[index + 2] & 0xff) << 40)  
                | (((long) bb[index + 3] & 0xff) << 32)  
                | (((long) bb[index + 4] & 0xff) << 24)  
                | (((long) bb[index + 5] & 0xff) << 16)  
                | (((long) bb[index + 6] & 0xff) << 8) | (((long) bb[index + 7] & 0xff) << 0));  

	}
	
	public static int putLong(byte[] src, int index, long b){
		
		for (int i = 0; i < 8; i++) {

			src[index + i] = (byte) (b >> 8 * (7 - i) & 0xFF);

		}

		return index + 8;
	}
	
	public static int putInt(byte[] src, int index, int b) {

		for (int i = 0; i < 4; i++) {

			src[index + i] = (byte) (b >> 8 * (3 - i) & 0xFF);

		}

		return index + 4;
	}
	
	public static int putFloat(byte[] src,int index,float f){
		
		int intBits = Float.floatToRawIntBits(f);
		
		putInt(src, index, intBits);
		
		return index + 4;
	}
	
	// 读取float
	public static float readFloat(byte[] src,int off){
		
		int intBits = readInt(src, off);
		
		return Float.intBitsToFloat(intBits);
	}
	
	public static int putDouble(byte[] src,int index,double d){
		
		long longBits = Double.doubleToLongBits(d);
		
		return putLong(src, index, longBits);
	}
	
	public static double readDouble(byte[] src,int off){
		
		long longBits = readLong(src, off);
		
		return Double.longBitsToDouble(longBits);
	}
	
	/**
	 * 
	 * @param src
	 *            目标源
	 * @param index
	 *            开始下标
	 * @param data
	 *            数据源
	 * @param length
	 *            添加长度
	 * @return 下一次添加的角标
	 */
	public static int put(byte[] src, int index, byte[] data, int off,
			int length) {

		for (int i = 0; i < length; i++) {

			src[index + i] = data[off + i];
		}

		return index + length;
	}

	/**
	 * 
	 * @param src
	 *            目标源
	 * @param index
	 *            开始下标
	 * @param data
	 *            数据源
	 * @return 下一次添加的角标
	 */
	public static int put(byte[] src, int index, byte[] data) {

		for (int i = 0; i < data.length; i++) {

			src[index + i] = data[i];
		}

		return index + data.length;

	}
	
	public static byte[] combatAll(List<byte[]> data,int dataByteSize){
		
		int index = 0;
		
		byte[] src = new byte[dataByteSize];
		
		for (byte[] bb : data) {
			
			index = put(src, index, bb);
		}
		
		return src;
	}
	
	public static void main(String[] args) {
		
		double d = 2.3242343243;
		
		byte[] src = new byte[8];
		
		putDouble(src, 0, d);
		
		System.out.println(readDouble(src, 0));
	}
	
}
